package net.huashitong.Indicators.web;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.util.RedisUtils;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dto.BA01DTO;
import net.huashitong.Indicators.dto.StudentDTO;
import net.huashitong.Indicators.entity.AB01;
import net.huashitong.Indicators.entity.AB02;
import net.huashitong.Indicators.entity.BA01;
import net.huashitong.Indicators.filter.BA01Filter;
import net.huashitong.Indicators.service.*;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("sys/ownerCourse")
public class OwnerCourseCtrl extends JsonBaseController {

    @Autowired
    private AB02Service studentService;
    @Autowired
    private BA01Service ba01Service; //课程表
    @Autowired
    private AB01Service ab01Service;//学生信息
    @Autowired
    private BattingService service;
    @Autowired
    private AB02Service ab02Service;//学生信息

    @Autowired
    private RedisService redisService;

    @Autowired
    private ScheJobService ser;

    @Override
    protected String getView(String s) {
        return "modules/sys/ownCourse/"+s;
    }

    @RequestMapping(method= RequestMethod.GET)
    public String initPage(Model model)
    {
        AB01 ab01  = ab01Service.getAAb999(UserUtils.getUserId());
        model.addAttribute("aab999",ab01.getAAB999());

        return getView("query");
    }

    //初始化数据
    @ResponseBody
    @RequestMapping(method=RequestMethod.POST)
    public JqGrid query (@ModelAttribute StudentDTO dto )
    {
        String userId = UserUtils.getUserId();
       // List<BA01> ba01s = ba01Service.getAllCourse(filter);
       dto.setAAB998(userId);
       List<StudentDTO> dtos = studentService.getOwerCourse(dto);




        return coverJqGrid(dto, dtos);
    }


    @RequestMapping(value = "/edit/{id}")
    public String eidtCours(Model model,@PathVariable String  id)
    {


        StudentDTO dto =new StudentDTO();
        dto.setSysId(id);
        Paging paging = new Paging();
        paging.setPageSize(28);
        paging.setCurrentPage(1);
        dto.setPaging(paging);
        List<StudentDTO> dtos = studentService.getOwerCourse(dto);

       dto.setSysId(null);

        dto.setBAB001(dtos.get(0).getBAB001());
        List<StudentDTO> stulist = ab02Service.getCourseRank(dto);
        //model.addAttribute("rankList",stulist);
        model.addAttribute("pageCount",dto.getPaging().getPageCount());


        model.addAttribute("course",dtos.get(0));
        dto.setAAB998(UserUtils.getUserId());
        List<StudentDTO> rank  = ab02Service.getRank2(dto);

        model.addAttribute("stuRank",ObjectUtils.isBlank(rank)?"-":rank.get(0).getAAB101());
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Message beattingCourse(@ModelAttribute BA01DTO ba01DTO)
    {

        String code = redisService.get("resultCode");
        if("1".equals(code))
        {
            return coverMessage("500","选课已经结束");
        }

        String userid =  SysUserUtils.getUserId();
        AB01 student  = ab01Service.queryStudent(null,userid);

        ba01DTO.setAAB001(student.getAAB001());
        ba01DTO.setAAB998(userid);
        AB02 betting  = ab02Service.getBetRecord(student.getAAB001(),ba01DTO.getBAB001(),userid);

        if(ObjectUtils.isNotBlank(betting))
        {
            try {
                service.updateAb02(betting,ba01DTO.getAAB996(),userid);
            } catch (Exception e) {
                return coverMessage("500","修改失败,余额不足");
            }
        }else {
            try {
                service.updateMemey(ba01DTO.getAAB996(),userid,null);
            } catch (Exception e) {
                return coverMessage("500","投注失败,余额不足");
            }
            service.saveAb02s(ba01DTO);
        }


        return  coverMessage("200");
    }


    @ResponseBody
    @RequestMapping(value = "/overgame",method = RequestMethod.POST)
    public Message overGame(@ModelAttribute BA01DTO ba01DTO)
    {
       /* String datetime = Until.getCurrentDate();
        String temdate  = redisService.get("overdate");
        if(StringUtils.isBlank(temdate))*/
        redisService.set("overdate","20181030");

        //ab02Service.batchAccount("K001","20");

        return coverMessage("200","ok");
    }


    @ResponseBody
    @RequestMapping(value = "/backCourse",method = RequestMethod.POST)
    public Message backCourse(@ModelAttribute BA01DTO ba01DTO) {


        String code = redisService.get("resultCode");
        if("1".equals(code))
        {
            return coverMessage("500","选课已经结束");
        }
        String userid = SysUserUtils.getUserId();
        AB01 student = ab01Service.queryStudent(null, userid);

        ba01DTO.setAAB998(userid);
        ba01DTO.setAAB001(student.getAAB001());
        ba01DTO.setBAB003(ba01DTO.getBAB003());
        AB02 betting = ab02Service.getBetRecord(student.getAAB001(), ba01DTO.getBAB001(), userid);

        service.updateAcountBack(betting, "0", userid);

        return  coverMessage("200");
    }

}
